package com.kingdee.eas.jky.pm.app;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.DefineProjectFactory;
import com.kingdee.eas.custom.DefineProjectInfo;
import com.kingdee.eas.custom.IDefineProject;
import com.kingdee.eas.jky.business.IIntradeSchemeConfirmation;
import com.kingdee.eas.jky.business.IntradeSchemeConfirmationCollection;
import com.kingdee.eas.jky.business.IntradeSchemeConfirmationFactory;
import com.kingdee.eas.jky.business.IntradeSchemeConfirmationInfo;
import com.kingdee.eas.jky.crm.ContractManageFactory;
import com.kingdee.eas.jky.crm.IContractManage;
import com.kingdee.eas.jky.pm.EarningAdjustFactory;
import com.kingdee.eas.jky.pm.EarningAdjustInfo;
import com.kingdee.eas.jky.pm.IEarningAdjust;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.eas.xr.app.XRBillStatusEnum;

public class EarningAdjustControllerBean extends AbstractEarningAdjustControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.jky.pm.app.EarningAdjustControllerBean");
    protected void _audit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._audit(ctx, pk);
         EarningAdjustInfo AffirmInfo = (EarningAdjustInfo) this.getValue(ctx, pk);
         if(AffirmInfo.getProjectNumber()!=null){
        	 String projectID=AffirmInfo.getProjectNumber().getId().toString();
        	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
        	 projectInfo.setYjSrAmount(AffirmInfo.getYijsAmount());
			 projectInfo.setRateAmount(AffirmInfo.getTzhsr());
			 projectInfo.setYjtotalAmount(AffirmInfo.getTzhhj());
			 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
			 StringBuilder sql = new StringBuilder();
			 sql.append(" update T_PM_ProjectBudget set FContractAmount = '"+ AffirmInfo.getYijsAmount() +"' where FProjectNumberID = '"+projectID+"'" );
			 DbUtil.execute(ctx, sql.toString());
			
			 IIntradeSchemeConfirmation IntradeScheme = IntradeSchemeConfirmationFactory.getLocalInstance(ctx);
	         IDefineProject Project =DefineProjectFactory.getLocalInstance(ctx);
	         IContractManage ContractManage =ContractManageFactory.getLocalInstance(ctx);
	         if(AffirmInfo.getProjectNumber()!=null){
	        	 EntityViewInfo evInfo1 = new EntityViewInfo();
	 	    	 FilterInfo filter1 = new FilterInfo();
	 	    	 filter1.getFilterItems().add(new FilterItemInfo("projectNum.id",projectInfo.getId().toString()));
	 	    	 evInfo1.setFilter(filter1);
	 	    	 IntradeSchemeConfirmationCollection coll =IntradeScheme.getIntradeSchemeConfirmationCollection(evInfo1);
	 	    	 if(coll.size()>0){
	 	    		IntradeSchemeConfirmationInfo SchemeInfo= coll.get(0);
	 	    		 projectInfo.setNbjyjr(projectInfo.getYjSrAmount().multiply(SchemeInfo.getIntradePricePer()).divide(new BigDecimal(100), 2, 4));
		        	 Project.update(new ObjectUuidPK(SchemeInfo.getProjectNum().getId()), projectInfo);
	 	    	 }
	         }
			 
         }
         
    }
    protected void _unAudit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pk);
    }
    protected void _unAudit(Context ctx, IObjectPK[] pks)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pks);
    }
	protected void _back(Context ctx, IObjectValue model) throws BOSException {
		 IEarningAdjust  budget =EarningAdjustFactory.getLocalInstance(ctx);
		 EarningAdjustInfo infos = (EarningAdjustInfo)model;
		 EarningAdjustInfo info;
		try {
			info = budget.getEarningAdjustInfo(new ObjectUuidPK(infos.getId()));
			info.setStatus(XRBillStatusEnum.TEMPORARILYSAVED);
			budget.update(new ObjectUuidPK(info.getId()), info);
		} catch (EASBizException e) {
			e.printStackTrace();
		}
	}
		
}